package com.cnbot.irobotvoice.api;

import android.util.Log;

import com.cnbot.irobotvoice.listener.IRobotAsrResultListener;
import com.xiaoi.why.sdk.utils.callback.ASRCallback;
import com.xiaoi.why.sdk.vcloud.asr.ASRManager;


/**
 * Copyright (c) 2016--2019/2/21  Hunan Cnbot Co., Ltd. All Rights Reserved.
 *
 * @descriptoin 小i，语音识别工具类
 * @FileName: IRobotAsrTools.java
 * @author: dc
 * @date: 2019/2/21 10:07
 * @version: 1.0
 */

public class IRobotAsrTools {
    private final String TAG = IRobotAsrTools.class.getSimpleName();

    private static IRobotAsrTools instance = null;
    /**
     * 语音识别引擎
     */
    private ASRManager asrManager;

    /**
     * 语音识别结果监听器
     */
    private IRobotAsrResultListener iRobotAsrResultListener = null;

    /**
     * 正式运行服务器地址
     */
    String RELEASE_BASE_ASR_URL = "10.255.30.97";

    /**
     * debug运行服务器地址
     */
    String DEBUG_BASE_ASR_URL = "vcloud.xiaoi.com";

    /**
     * 服务器地址, 根据buildConfig的变量DEBUG_SERVER_URL 获取不同的服务器地址
     */
    String ASR_URL;


    public static IRobotAsrTools getInstance(boolean isDebug){
        if(instance == null){
            synchronized (IRobotAsrTools.class){
                if(instance == null){
                    instance = new IRobotAsrTools(isDebug);
                }
            }
        }
        return instance;
    }

    public IRobotAsrTools(boolean isDebugUrl) {
        ASR_URL = isDebugUrl == false ? "http://" + RELEASE_BASE_ASR_URL + "/recog.do" : "http://" + DEBUG_BASE_ASR_URL + "/recog.do";
    }

    /**
     * 初始化语音识别，默认中文识别
     */
    public void asrInit(){
        asrManager = new ASRManager.Builder()
                .appKey("9516d09e23e34ae38687fc6b2e038d7c")
                .appSecret("0fd08ffd50944af5811cc1412d682a2e")
                .url(ASR_URL)
                .signature("recog.do")
                .autoEnd(false)
                .build();
        Log.e(TAG, "ASR_URL = " + ASR_URL);
    }

    /**
     * 初始化语音识别，英文识别
     */
    public void asrInitEnglish(){
        asrManager = new ASRManager.Builder()
                .appKey("e025f4367a414609a2cd22e0bb22fbc2")
                .appSecret("627da2ccf18e4584b7b217f2f2a0912d")
                .url("http://vcloud.xiaoi.com/recog.do")
                .signature("recog.do")
                .autoEnd(true)
                .build();
    }

    /**
     * 开始识别
     */
    public void startAsr(){
        //内置录音
        if(asrManager != null) {
            asrManager.startRecord(new AsrResultCallback());
        }
    }

    /**
     * 结束录音，等待识别结果
     */
    public void stopAsr(){
        if(asrManager != null) {
            asrManager.stopRecord();
        }
    }

    /**
     * 用于取消一次请求(用于结束录音之后还没结果之前)
     */
    public void cancalAsr(){
        if(asrManager != null) {
            asrManager.cancelASR();
        }
    }

    /**
     * 注册结果监听器
     * @param iRobotAsrResultListener
     */
    public void setiRobotAsrResultListener(IRobotAsrResultListener iRobotAsrResultListener) {
        this.iRobotAsrResultListener = iRobotAsrResultListener;
    }

    /**
     * 移除监听器
     */
    public void removeiRobotAsrResultListener() {
        this.iRobotAsrResultListener = null;
    }


    /**
     * Asr语音识别结果
     */
    public class AsrResultCallback implements ASRCallback {
        @Override
        public void onStartRecord() {
            Log.e(TAG, "onStartRecord: 开始录音 Thread: " + Thread.currentThread().getName());
            if(iRobotAsrResultListener != null) {
                iRobotAsrResultListener.asrBefore();
            }
        }

        @Override
        public void onStopRecord() {
            Log.e(TAG, "onStopRecord: 结束录音 Thread: " + Thread.currentThread().getName());
            if(iRobotAsrResultListener != null) {
                iRobotAsrResultListener.asrEnd();
            }
        }

        @Override
        public void onCancelRecord() {
            Log.e(TAG, "onCancelRecord: 取消录音 Thread: " + Thread.currentThread().getName());
            if(iRobotAsrResultListener != null) {
                iRobotAsrResultListener.asrEnd();
            }
        }

        @Override
        public void onVoiceData(byte[] bytes, int i) {
            Log.e(TAG, "onVoiceData: 音频数据 Thread: " + Thread.currentThread().getName());
        }

        @Override
        public void onASRResult(String s1) {
            Log.e(TAG, "onASRResult: 语音识别结果 Thread: " + s1 + Thread.currentThread().getName());
            if(iRobotAsrResultListener != null) {
                iRobotAsrResultListener.asrResultFinal(s1);
            }
        }

        @Override
        public void onASRError(String s) {
            Log.e(TAG, "onASRError: 录音出错 " + s + Thread.currentThread().getName());
            if(iRobotAsrResultListener != null) {
                iRobotAsrResultListener.asrError(s);
            }
        }
    }

}

